package com.zut.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zut.common.BaseContext;
import com.zut.common.CustomException;
import com.zut.entity.*;
import com.zut.mapper.OrderMapper;
import com.zut.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

@Service
public class OrderSericeImpl extends ServiceImpl<OrderMapper,Orders> implements OrderService{

    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private UserService userService;

    @Autowired
    private AddressBookService addressBookService;

    @Autowired
    private OrderDetailService orderDetailService;
    @Override
    @Transactional
    public void submit(Orders orders) {
        // 获取用户id
        Long id= BaseContext.getCurrentId();
        // 获取购物车数据
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,id);
        List<ShoppingCart> list=shoppingCartService.list(queryWrapper);
         if(list==null||list.size()==0){
             throw new CustomException("购物车为空，不能下单");
         }
         //获取用户数据
        User u=userService.getById(id);
        //获取地址数据
        AddressBook addressBook=addressBookService.getById(orders.getAddressBookId());
        if(addressBook==null||addressBook.getIsDeleted()==1){
            throw new CustomException("地址信息有误，不能下单");
        }
        Long orderId= IdWorker.getId();
        AtomicInteger amount=new AtomicInteger(0);
       List<OrderDetail> orderDetails= list.stream().map(item->{
           OrderDetail orderDetail=new OrderDetail();
           orderDetail.setOrderId(orderId);
           orderDetail.setNumber(item.getNumber());
           orderDetail.setDishFlavor(item.getDishFlavor());
           orderDetail.setDishId(item.getDishId());
           orderDetail.setSetmealId(item.getSetmealId());

           orderDetail.setName(item.getName());
           orderDetail.setImage(item.getImage());
           orderDetail.setAmount(item.getAmount());
           amount.addAndGet(item.getAmount().multiply(new BigDecimal(item.getNumber())).intValue());
           return orderDetail;
       }).collect(Collectors.toList());
        // 保存订单数据

        orders.setId(orderId);
        orders.setNumber(String.valueOf(orderId));
        orders.setStatus(2);
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setAmount(new BigDecimal(amount.get()));
        orders.setUserId(id);
        orders.setUserName(u.getName());

        orders.setAddress((addressBook.getProvinceName()==null?"":addressBook.getProvinceName())+(addressBook.getCityName()==null?"":addressBook.getCityName())+(addressBook.getDistrictName()==null?"":addressBook.getDistrictName())+(addressBook.getDetail()==null?"":addressBook.getDetail()));
        orders.setPhone(u.getPhone());
        orders.setConsignee(addressBook.getConsignee());

        this.save(orders);

        // 保存订单明细数据
        orderDetailService.saveBatch(orderDetails);
        // 清空购物车数据
        shoppingCartService.remove(queryWrapper);
    }
}
